package com.laicunba.ext.activity.act20170703;

import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.ActivityJoinEvent;
import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.ActivityJoinDO;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.service.ActivityJoinService;
import com.laicunba.service.ActivityService;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.RateIncrCouponDefService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.VcoinWalletService;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

//@Component
public class Act20170703Listener implements ApplicationListener<ActivityJoinEvent> {
	@Autowired
	private VcoinWalletService vcoinWalletService;
	@Autowired
	private CashCouponDefService cashCouponDefService;
	@Autowired
	private RateIncrCouponDefService rateIncrCouponDefService;
	@Autowired
	private ActivityJoinService activityJoinService;
	@Autowired
	private RegistryService registryService;
	@Autowired
	private ActivityService activityService;
	private static int HOUR24 = 3600 * 24;

	@Override
	public void onApplicationEvent(ActivityJoinEvent event) {
		ActivityJoinDO joinDO = (ActivityJoinDO) event.getSource();
		if (!"20170703".equalsIgnoreCase(joinDO.getActivityId())) {
			return;
		}
		
		ActivityDO activity = activityService.get(ActivityDO.class, joinDO.getActivityId());
		if(activity == null || activity.getBeginTime().after(new Date()) || activity.getEndTime().before(new Date())) {
			return;
		}

		if (joinDO != null && StringUtil.isNotEmpty(joinDO.getUserId(), joinDO.getSubActId())) {
			if ("001".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-celebrate-depository-red-package-50");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"庆存管上线活动兑换现金券");
					joinDO.setRewardResp("庆存管上线活动，积分兑换50元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 5888, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换50元现金券");
				}
			} else if ("002".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("庆存管上线活动，积分兑换50元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 5888, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换50元京东E卡");
			} else if ("003".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-celebrate-depository-red-package-150");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"庆存管上线活动兑换现金券");
					joinDO.setRewardResp("庆存管上线活动，积分兑换150元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 15000, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换150元现金券");
				}
			} else if ("004".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("庆存管上线活动，积分兑换200元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 20000, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换200元京东E卡");
			} else if ("005".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-celebrate-depository-red-package-300");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"庆存管上线活动兑换现金券");
					joinDO.setRewardResp("庆存管上线活动，积分兑换300元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 25000, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换300元现金券");
				}
			} else if ("006".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-celebrate-depository-red-package-500");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"庆存管上线活动兑换现金券");
					joinDO.setRewardResp("庆存管上线活动，积分兑换500元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 41500, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换500元现金券");
				}
			} else if ("007".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-celebrate-depository-red-package-700");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"庆存管上线活动兑换现金券");
					joinDO.setRewardResp("庆存管上线活动，积分兑换700元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 50000, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换700元现金券");
				}
			} else if ("008".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("庆存管上线活动，积分兑换Ipad mini 4（128G）");
				vcoinWalletService.subtract(joinDO.getUserId(), 220000, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换Ipad mini 4");
			} else if ("009".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("庆存管上线活动，积分兑换Iphone7（红色、128G）");
				vcoinWalletService.subtract(joinDO.getUserId(), 375000, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换Iphone7");
			} else if ("010".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("庆存管上线活动，积分兑换MacBook Air13.3英寸");
				vcoinWalletService.subtract(joinDO.getUserId(), 390000, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间成功兑换MacBook Air");
			} else if ("011".equals(joinDO.getSubActId())) {
				String regChanceKey = "act.20170703chance." + joinDO.getUserId() + ".regist";
				String cjCountKey = "act.20170703CJ." + joinDO.getUserId() + "." + DateUtil.formatDate(new Date());
				String chanceKey = "act.20170703chance." + joinDO.getUserId()+ "." + DateUtil.formatDate(new Date());
				int regChance = StringUtil.isNotEmpty(CacheUtil.getAttribute(regChanceKey)) ? Integer.parseInt(CacheUtil.getAttribute(regChanceKey)) : 0 ;
				int todayTotalChance = StringUtil.isNotEmpty(CacheUtil.getAttribute(chanceKey)) ? Integer.parseInt(CacheUtil.getAttribute(chanceKey)) : 0 ;
				int todayCjCount = StringUtil.isNotEmpty(CacheUtil.getAttribute(cjCountKey)) ? Integer.parseInt(CacheUtil.getAttribute(cjCountKey)) : 0 ;
				
				//A:0.5%加息券；B:5元现金券；C：10元现金券；D:20元现金券；E:30元现金券；F:1%加息券；G:1.5%加息券；
				char rewardResp = '0';
				if(regChance > 0) {
					//AB概率平均
					rewardResp = (char) (new Random().nextInt(2) + 65);
					CacheUtil.setAttribute(regChanceKey, "0");
				} else if(todayTotalChance - todayCjCount > 0) {
					//ABC概率平均
					rewardResp = (char) (new Random().nextInt(3) + 65);
					addHitCount(joinDO.getUserId());
				} else if(vcoinWalletService.getBalance(joinDO.getUserId()) >= 300) {
					//A:30%；B:20%；C:15%；D:10%；E:5%；F:15%；G:5%；
					rewardResp = StringUtil.tryLuck("AAAAAABBBBCCCDDEFFFG");
					vcoinWalletService.subtract(joinDO.getUserId(), 300, "CELEBRATE_DEPOSITORY_ONLINE", "庆存管上线活动期间参与摇奖");
				}
				
				String couponId = "";
				switch (rewardResp) {
					case 'A':
						couponId = "act-celebrate-depository-rate-incr-0.5";
						break;
					case 'B':
						couponId = "act-celebrate-depository-red-package-5";
						break;
					case 'C':
						couponId = "act-celebrate-depository-red-package-10";
						break;
					case 'D':
						couponId = "act-celebrate-depository-red-package-20";
						break;
					case 'E':
						couponId = "act-celebrate-depository-red-package-30";
						break;
					case 'F':
						couponId = "act-celebrate-depository-rate-incr-1";
						break;
					case 'G':
						couponId = "act-celebrate-depository-rate-incr-1.5";
						break;
					default:
						couponId = "";
						break;
				}
				if(rewardResp == 'B' || rewardResp == 'C' || rewardResp == 'D' || rewardResp == 'E') {
					cashCouponDefService.send2UserWithMessage(couponId, joinDO.getUserId(), false,"庆存管上线活动抽奖现金券");
				} else if(rewardResp == 'A' || rewardResp == 'F' || rewardResp == 'G') {
					rateIncrCouponDefService.send2User(couponId, joinDO.getUserId(), false, "庆存管上线活动抽奖加息券");
				}
				joinDO.setRewardResp(String.valueOf(rewardResp));
				joinDO.setStatus("success");
			}
			AssertUtil.assertTrue(activityJoinService.updateSkipCheckOwner(joinDO), "REWARD_FAIL");
		}
	}
	
	public void addHitCount(String userId) {
		String cjCountKey = "act.20170703CJ." + userId + "." + DateUtil.formatDate(new Date());
		CacheUtil.incr(cjCountKey, HOUR24);
	}

}
